﻿/*
* 
* TEST = OK
*/
Library.UserInterface.GetDetailPageElement = function(elementId) {
    if (elementId == "")
        return;
    if (document.getElementById('ctl00__masterPagePrefix') && document.getElementById(document.getElementById('ctl00__masterPagePrefix').value + elementId))
        return document.getElementById(document.getElementById('ctl00__masterPagePrefix').value + elementId);
    if (document.getElementById(elementId))
        return document.getElementById(elementId);
    return null;
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Set = function (elementId, elementValue) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    if (element == null)
        element = document.getElementById(elementId);

    if (element == null)
        return;

    var controlType = Library.UserInterface.GetAttribute(elementId, "ControlType");
    if (controlType != null && controlType == "Combo")
        element.value = elementValue;
    else if (controlType != null && controlType == "DivList")
        element.innerHTML += "<span class='DragableContent' value='" + elementValue.split('|')[1] + "'>" + elementValue.split('|')[0] + "</span>";
    else if (element.type == "select")
        element.selectedValue = elementValue;
    else if (element.type == "div")
        element.innerHTML = elementValue;
    else if (element.type == "span")
        element.innerText = elementValue;
    else if (element.type == "textarea" && tinyMCE.get(elementId) != null)
        tinyMCE.activeEditor.setContent(elementValue);
    else
        element.value = elementValue;
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Get = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    var controlType = Library.UserInterface.GetAttribute(elementId, "ControlType");
    if (controlType != null && controlType == "Combo")
        return element.value;
    else if (element.type == "select")
        return element.selectedValue;
    else if (element.type == "div")
        return element.innerHTML;
    else if (element.type == "span")
        return element.innerText;
    else
        return element.value;
};

/*
* 
* TEST = OK
*/
Library.UserInterface.GetAttribute = function (elementId, attributeName) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);    
    if (eval("element." + attributeName))
        return eval("element." + attributeName) == null ? "" : eval("element." + attributeName);
    else if (element.getAttribute(attributeName))
        return element.getAttribute(attributeName);
    else
        return "";
};

/*
* 
* TEST = OK
*/
Library.UserInterface.SetAttribute = function(elementId, attributeName, value) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    if (eval("element." + attributeName))
        eval("element." + attributeName + "=" + value + ";");
    else
        element.setAttribute(attributeName, value);
};

/*
* 
* TEST = OK
*/
Library.UserInterface.SetCursorPositionToEnd = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    var selectionEnd = element.value.length;
    var selectionStart = element.value.length;
    if (element.createTextRange) {
        var range = element.createTextRange();
        range.collapse(true);
        range.moveEnd('character', selectionEnd);
        range.moveStart('character', selectionStart);
        range.select();
    } else if (element.setSelectionRange) {
        element.focus();
        element.setSelectionRange(selectionStart, selectionEnd);
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.ElementPosition = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    var position = new Array();
    position[0] = 0;
    position[1] = 0;

    if (element.offsetParent) {
        while (1) {
            position[0] += element.offsetTop;
            position[1] += element.offsetLeft;
            if (!element.offsetParent)
                break;
            element = element.offsetParent;
        }
    } else if (element.x) {
        position[0] += element.y;
        position[1] += element.x;
    }
    return position;
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Hide = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    if (element.style.display != "none") {
        Library.UserInterface.SetAttribute(elementId, "ShowProperty", "ClientSide");
        element.style.display = "none";
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Show = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    if (element && element.style.display == "none" && Library.UserInterface.GetAttribute(elementId, "ShowProperty") == "ClientSide") {
        Library.UserInterface.SetAttribute(elementId, "ShowProperty", "");
        element.style.display = "";
    }    
};

/*
* 
* TEST = OK
*/
Library.UserInterface.HideByType = function(elementType, elementSubType) {
    var elements = document.getElementsByTagName(elementType);
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].id == "")
            continue;
        if (elementSubType != null && elementSubType != Library.UserInterface.GetAttribute(elements[i].id, "type"))
            continue;
        if (elements[i].style.display != "none") {
            Library.UserInterface.SetAttribute(elements[i].id, "ShowProperty", "ClientSide");
            elements[i].style.display = "none";
        }
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.ShowByType = function(elementType, elementSubType) {
    var elements = document.getElementsByTagName(elementType);
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].id == "")
            continue;
        if (elementSubType != null && elementSubType != Library.UserInterface.GetAttribute(elements[i].id, "type"))
            continue;
        if (elements[i].style.display == "none" && Library.UserInterface.GetAttribute(elements[i].id, "ShowProperty") == "ClientSide") {
            Library.UserInterface.SetAttribute(elements[i].id, "ShowProperty", "");
            elements[i].style.display = "";
        }
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Disable = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    if (element && !element.disabled) {
        Library.UserInterface.SetAttribute(elementId, "DisableProperty", "ClientSide");
        element.disabled = true;
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Enable = function(elementId) {
    var element = Library.UserInterface.GetDetailPageElement(elementId);
    if (element && element.disabled && Library.UserInterface.GetAttribute(elementId, "DisableProperty") == "ClientSide") {
        Library.UserInterface.SetAttribute(elementId, "DisableProperty", "");
        element.disabled = false;
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.DisableByType = function(elementType, elementSubType) {
    var elements = document.getElementsByTagName(elementType);
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].id == "")
            continue;
        if (elementSubType != null && elementSubType != Library.UserInterface.GetAttribute(elements[i].id, "type"))
            continue;
        if (!elements[i].disabled) {
            Library.UserInterface.SetAttribute(elements[i].id, "DisableProperty", "ClientSide");
            elements[i].disabled = true;
        }
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.EnableByType = function(elementType, elementSubType) {
    var elements = document.getElementsByTagName(elementType);
    for (var i = 0; i < elements.length; i++) {
        if (elements[i].id == "")
            continue;
        if (elementSubType != null && elementSubType != Library.UserInterface.GetAttribute(elements[i].id, "type"))
            continue;
        if (elements[i].disabled && Library.UserInterface.GetAttribute(elements[i].id, "DisableProperty") == "ClientSide") {
            Library.UserInterface.SetAttribute(elements[i].id, "DisableProperty", "");
            elements[i].disabled = false;
        }
    }
};

/*
* 
* TEST = OK
*/
Library.UserInterface.Submit = function (event, url, islem) {
    var queryString = document.forms[0].action.split("?")[1];
    if (url == null || url == "") {
        url = document.forms[0].action.split("?")[0];

        if (islem.indexOf("{") > -1) {
            var parameter = islem.split("{")[1].split("}")[0];
            islem = Library.Util.ReplaceAll(islem, "{" + parameter + "}", Library.UserInterface.Get(parameter));
        }
        if (queryString != null && queryString != "undefined" && queryString.indexOf("ShowHeader=true") > -1)
            document.forms[0].action = url + "?ShowHeader=true&islem=" + islem;
        else
            document.forms[0].action = url + "?islem=" + islem;

        //document.forms[0].submit();
    } else {
        if (islem.indexOf("{") > -1) {
            var parameter = islem.split("{")[1].split("}")[0];
            islem = Library.Util.ReplaceAll(islem, "{" + parameter + "}", Library.UserInterface.Get(parameter));
        }

        if (url.indexOf("?") > -1)
            url += "&";
        else
            url += "?";

        url += "islem=" + islem;

        document.location = url;
        return Library.Util.Return(event, false);
    }
};